<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('区块链信息')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="panel panel-default">
            <div class="panel-heading">
                <button th:text="启动挖矿" class="btn btn-success" th:onclick="mining()"/>
                <span/>
                <button th:text="停止挖矿" class="btn btn-primary" th:onclick="shutdownmining()"/>
            </div>
        </div>
        <br/>
        <div class="panel panel-default">
            <div class="panel-heading">
                <button th:text="获取节点列表" class="btn btn-primary" th:onclick="view()"/>
                <span/>
            </div>
            <table class="table">
                <thead id="walletlist">
                <tr>
                    <th>
                        地址
                    </th>
                    <th>
                        端口
                    </th>
                </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
        <br/>
    </div>

</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">


    function mining() {
        $.ajax({
            type: "GET",
            url: "http://localhost/api/chain/mining",
            dataType: "json",
            success: function (data) {
                if (data.message == "Create a new block") {
                    alert("启动成功");
                }
            },
            error: function (data) {
                alert("启动失败");
            }
        });
    }

    function shutdownmining() {
        $.ajax({
            type: "GET",
            url: "http://localhost/api/chain/shutdownmining",
            dataType: "json",
            success: function (data) {
                if (data.message == "shutdown mining is success") {
                    alert("停止成功");
                }
            },
            error: function (data) {
                alert("停止失败");
            }
        });
    }

    function view() {
        $.ajax({
            type: "POST",
            url: "http://localhost/api/chain/node/view",
            dataType: "json",
            success: function (data) {
                if (data.message == "SUCCESS" && data.item.length != 0) {
                    $("#walletlist").nextAll().remove();
                    for (var x = 0; x < data.item.length; x++) {
                        console.log(data.item[x]);
                        $("#walletlist").after("<tr>\n" +
                            "                    <th>\n" +
                            "                        " + data.item[x].ip + "\n" +
                            "                    </th>\n" +
                            "                    <th>\n" +
                            "                        " + data.item[x].port + "\n" +
                            "                    </th>\n" +
                            "                </tr>");
                    }
                }
            }
        });
    }


</script>
</body>
</html>